package com.example.administrator.energyida.adapter;

import android.content.ClipData;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.energyida.R;

import java.security.acl.Group;
import java.util.ArrayList;
import java.util.HashMap;


public class ItemExpandableFactoryAdapter extends BaseExpandableListAdapter implements View.OnClickListener {

    private ArrayList<HashMap<String, String>> gData;
    private ArrayList<ArrayList<HashMap<String, String>>> iData;
    private Context mContext;
    private Callback mCallback;

    public interface Callback {
        public void click(View view);
    }

    public ItemExpandableFactoryAdapter(ArrayList<HashMap<String, String>> gData, ArrayList<ArrayList<HashMap<String, String>>> iData, Context mContext, Callback mCallback) {
        this.gData = gData;
        this.iData = iData;
        this.mContext = mContext;
        this.mCallback = mCallback;
    }

    @Override
    public int getGroupCount() {
        return gData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return iData.get(groupPosition).size();
    }

    @Override
    public Group getGroup(int groupPosition) {
        return null;
    }

    @Override
    public ClipData.Item getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    //取得用于显示给定分组的视图. 这个方法仅返回分组的视图对象
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {


        ViewHolderGroup groupHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_parent_listview_factoryinfo, parent, false);
            groupHolder = new ViewHolderGroup();
            groupHolder.factoryName = (TextView) convertView.findViewById(R.id.factoryinfo_textView_factoryName);
            groupHolder.chargeInfo = convertView.findViewById(R.id.factoryInfo_textView_chargeInfo);
            groupHolder.addFarm = convertView.findViewById(R.id.factoryInfo_imageView_addFarm);
            groupHolder.editFactory = convertView.findViewById(R.id.factoryInfo_imageView_editFactory);
            convertView.setTag(groupHolder);
        }else{
            groupHolder = (ViewHolderGroup) convertView.getTag();
        }
        groupHolder.factoryName.setText(gData.get(groupPosition).get("fName"));
        groupHolder.chargeInfo.setText(gData.get(groupPosition).get("fMInfo"));
        groupHolder.addFarm.setOnClickListener(this);
        groupHolder.editFactory.setOnClickListener(this);
        groupHolder.addFarm.setTag(R.id.groupTag,groupPosition);
        groupHolder.editFactory.setTag(R.id.groupTag,groupPosition);

        ImageView icon = convertView.findViewById(R.id.factoryInfo_imageView_icon);

        if (isExpanded) {
            icon.setImageResource(R.drawable.ic_down);
        }
        if (!isExpanded) {
            icon.setImageResource(R.drawable.ic_right);
        }


        return convertView;
    }

    //取得显示给定分组给定子位置的数据用的视图
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderItem itemHolder;

        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_child_listview_factoryinfo, parent, false);
            itemHolder = new ViewHolderItem();
            itemHolder.farmName = (TextView) convertView.findViewById(R.id.farmInfo_textView_farmName);
            itemHolder.chargeInfo = convertView.findViewById(R.id.farmInfo_textView_cahrgeInfo);
            itemHolder.editFarm = convertView.findViewById(R.id.farmInfo_imageView_editFarm);
            convertView.setTag(itemHolder);
        }else{
            itemHolder = (ViewHolderItem) convertView.getTag();
        }
        itemHolder.farmName.setText(iData.get(groupPosition).get(childPosition).get("wName"));
        itemHolder.chargeInfo.setText(iData.get(groupPosition).get(childPosition).get("wMInfo"));
        itemHolder.editFarm.setOnClickListener(this);
        itemHolder.editFarm.setTag(R.id.groupTag, groupPosition);
        itemHolder.editFarm.setTag(R.id.childTag, childPosition);
        return convertView;
    }

    //设置子列表是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static class ViewHolderGroup{
        private TextView factoryName;
        private TextView chargeInfo;
        private ImageView addFarm;
        private ImageView editFactory;
        private ImageView icon;
    }

    private static class ViewHolderItem{
        private TextView farmName;
        private TextView chargeInfo;
        private ImageView editFarm;
    }

    @Override
    public void onClick(View v) {
        mCallback.click(v);
    }
}
